/**
 * Copyright (C) 2013  Mykola Bespaliuk
 */
var tinyurl = tinyurl || {};
/**
 * Pattern to match tiny url at the result page
 */
tinyurl.urlPatern = /\[<a\shref=['"](http:\/\/tinyurl.com\/[^'"]+)['"]/i;
/**
 * Base URL for making request
 */
tinyurl.requestUrl = "http://tinyurl.com/create.php?url=";
/**
 * Function that will be called after the result is parsed
 * and short link is ready
 */
tinyurl.callback   = null;
/**
 * Makes request to the TinyURL service
 * @param url
 */
tinyurl.makeTiny = function( url ) {
    var requestUrl = tinyurl.requestUrl + encodeURIComponent( url );
    request.get( { url: requestUrl, async: true }, tinyurl.parseResult );
};
/**
 * Retrieves the result url from the page? returned by tinyurl service
 * @param pageCode
 */
tinyurl.parseResult = function( pageCode ) {
    var result = pageCode.match( tinyurl.urlPatern );
    if( result != null ) {
        tinyurl.callback( result[1] );
    }

};
